package com.mall.shopping.services;
/*
 * @author 魏骏杰
 */

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Slf4j
@Component
@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Resource
    ContentConverter converter;


    @Override
    public HomePageResponse homepage() {

        HomePageResponse response = new HomePageResponse();

        try {
            Set<Panel> panelContents = panelMapper.findAllHomepagesPanelList();

            Iterator<Panel> iterator = panelContents.iterator();

            HashSet<PanelDto> panelDtos = new HashSet<>();

            while (iterator.hasNext()) {
                Panel panel = iterator.next();

                List<PanelContentItem> panelContentItems = panelMapper.selectAllItemsByPanelId(panel.getId());

                panel.setPanelContentItems(panelContentItems);

                panelDtos.add(converter.panen2Dto(panel));
            }

            response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IHomeServiceImpl occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
